import 'dart:math';

import 'package:flutter/material.dart';
import 'package:dio/dio.dart';

class DioExampleWidget extends StatefulWidget {
  const DioExampleWidget({super.key});

  @override
  State<DioExampleWidget> createState() => _DioExampleWidgetState();
}

class _DioExampleWidgetState extends State<DioExampleWidget> {
  String _abc = '';

  @override
  void initState() {
    // 获取订单列表信息
    getOrderList();
    super.initState();
  }

  // 演示dio插件的基本使用
  void getOrderList() async {
    // 1. 准备请求地址
    String url = 'https://api-vue-base.itheima.net/api/joke';

    try {
      // 2. 创建dio对象
      Dio dio = Dio();
      // 3. 发送网络请求
      Response res = await dio.get(url);
      // 4. 打印数据
      // log(res.data);
      print(res.data.toString());
      setState(() {
        _abc = res.data.toString();
      });
    } catch (e) {
      print("错误代码为: " + e.toString());
    }
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        backgroundColor: Colors.yellow,
        title: const Text('案例-DioExample'),
      ),
      body: Center(child: Text(_abc)),
    );
  }
}
